package io.renren.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Preconditions;
import io.renren.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.UpdateEntity;
import io.renren.service.UpdateService;
import io.renren.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-20 12:55:41
 */
@RestController
@RequestMapping("api/update")
public class UpdateController {
    @Autowired
    private UpdateService updateService;

    /**
     * 列表
     */
    @SysLog("查询Update")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = updateService.queryPage(params);

        return R.ok().put("page", page);
    }





    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        UpdateEntity update = updateService.selectById(id);

        return R.ok().put("data", update);
    }





    /**
     * 删除
     */
    @SysLog("删除update")
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        updateService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 根据项目查询update
     */
    @RequestMapping("/infoByCID")
    public R infoByCID(@RequestParam Map<String, Object> params){

        Preconditions.checkArgument(null != params.get("campaignId"), "活动ID不能为空");

        int campaignId = Integer.parseInt(params.get("campaignId").toString());

        Page<UpdateEntity> UpdateEntityPage = updateService.selectPage(new Query<UpdateEntity>(params).getPage(), new EntityWrapper<UpdateEntity>().eq("campaign_id", campaignId));

        List<UpdateEntity> records = UpdateEntityPage.getRecords();


        return R.ok().put("data", UpdateEntityPage);
    }


    /**
     * 列表
     */
    @RequestMapping("/listByUID")
    public R listByCid(@RequestParam Map<String, Object> params){

        Preconditions.checkArgument(null != params.get("userId"), "用户ID不能为空");

        int uid = Integer.parseInt(params.get("userId").toString());

        Page<UpdateEntity> updateEntityPage = updateService.selectPage(new Query<UpdateEntity>(params).getPage(), new EntityWrapper<UpdateEntity>().eq("user_id", uid));

        List<UpdateEntity> records = updateEntityPage.getRecords();



        return R.ok().put("data", updateEntityPage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UpdateEntity campaignUpdate){
        updateService.insert(campaignUpdate);

        return R.ok();
    }

}
